package com.diors.game.engine.thread;

import com.diors.commons.log.DiorsLogger;
import com.diors.commons.thread.PriorityThreadFactory;
import com.diors.commons.thread.SelecterThreadPoolExecutor;
import com.diors.commons.thread.SelecterWrapper;

/**
 * 线城池管理类
 * @author darren.ouyang
 * @date 2013-9-15
 */
public class ThreadPoolManager 
{
	static DiorsLogger log = DiorsLogger.getLogger(ThreadPoolManager.class);
	
	private static final class SingletonHolder
	{
		private static final ThreadPoolManager	INSTANCE	= new ThreadPoolManager();
	}

	public static ThreadPoolManager getInstance()
	{
		return SingletonHolder.INSTANCE;
	}
	
	/**
	 * 执行网络解包任务池
	 */
	private final SelecterThreadPoolExecutor networkPool;
	
	/**
	 * 执行游戏业务的定时任务
	 */
	//private final ScheduledThreadPoolExecutor scheduledPool;
	
	/**
	 * 执行关闭连接触发的任务
	 */
	//private final ScheduledThreadPoolExecutor disconnectionPool;
	
	/**
	 * 执行AI线程池
	 */
	//private final ScheduledThreadPoolExecutor actionPool;
	
	
	/**
	 * 执行任务的线程池
	 */
	//private final ThreadPoolExecutor instantPool;
	
	
	/**
	 * 长时间线程池
	 */
	//private final ThreadPoolExecutor longinstantPoll;
	
	private ThreadPoolManager()
	{
		networkPool = new SelecterThreadPoolExecutor(new PriorityThreadFactory("network Pool", Thread.NORM_PRIORITY));
		
		/*scheduledPool = new ScheduledThreadPoolExecutor(instantPoolSize, new PriorityThreadFactory("ScheduledPool", Thread.NORM_PRIORITY));
		scheduledPool.setRejectedExecutionHandler(new CommonsExecutionHandler());
		scheduledPool.prestartAllCoreThreads();
		
		actionPool = new ScheduledThreadPoolExecutor(instantPoolSize, new PriorityThreadFactory("ActionPool", Thread.NORM_PRIORITY));
		actionPool.setRejectedExecutionHandler(new CommonsExecutionHandler());
		actionPool.prestartAllCoreThreads();
		
		instantPool = new ThreadPoolExecutor(instantPoolSize, instantPoolSize, 6L, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(100000), new PriorityThreadFactory("instance Pool", Thread.NORM_PRIORITY));
		instantPool.setRejectedExecutionHandler(new CommonsExecutionHandler());
		instantPool.prestartAllCoreThreads();
		
		longinstantPoll = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 6L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), new PriorityThreadFactory("LongInstant Pool", Thread.NORM_PRIORITY));
		longinstantPoll.setRejectedExecutionHandler(new CommonsExecutionHandler());
	
		
		disconnectionPool = new ScheduledThreadPoolExecutor(4, new PriorityThreadFactory("DisconnectionPool", Thread.NORM_PRIORITY));
		disconnectionPool.prestartAllCoreThreads();
		//disconnectionPool.setRemoveOnCancelPolicy(true);*/
	}
	
	public void executeNetwork(SelecterWrapper task)
	{
		networkPool.execute(task);
	}
}
